package sing.rabbit.direct.provider.send;

import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import static sing.rabbit.direct.common.DirectConfigurations.DIRECT_ROUTING_KEY_DEV;

/**
 * @author xing
 * @date 2022/9/8 7:13
 */
@Component
public class DirectMessageSender {

    @Autowired
    private RabbitTemplate template;
    @Autowired
    private DirectExchange directExchange;

    /**
     * 定时循环发送一条 String 消息到 routingKey: dev
     */
    @Profile({"string-dev"})
    @Component
    class DirectStringToDevMessageSender {
        @Scheduled(fixedDelay = 1000, initialDelay = 500)
        public void sendStringToDev() {
            String message = "A Direct Message";
            template.convertAndSend(directExchange.getName(), DIRECT_ROUTING_KEY_DEV, message);
            System.out.println("send a string message : " + message);
        }
    }

    /**
     * 定时循环发送一条 Message 消息到 routingKey: dev
     */
    @Profile({"message-dev"})
    @Component
    class DirectMessageToDevMessageSender {
        @Scheduled(fixedDelay = 1000, initialDelay = 500)
        public void sendMessageToDev() {
            String messageContent = "A Direct Message";
            Message message = new Message(messageContent.getBytes());
            MessageProperties messageProperties = message.getMessageProperties();
            messageProperties.setHeader("token", "This is a token");
            messageProperties.setHeader("direct", "This is a direct type message");
            template.convertAndSend(directExchange.getName(), DIRECT_ROUTING_KEY_DEV, message);
            System.out.println("send a Message message : " + messageContent);
        }
    }
}
